package com.qfedu.edu.seckill.aclservice.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.edu.seckill.aclservice.mapper.PermMapper;
import com.qfedu.edu.seckill.aclservice.mapper.RoleMapper;
import com.qfedu.edu.seckill.aclservice.mapper.UserMapper;
import com.qfedu.edu.seckill.bo.SecurityUser;
import com.qfedu.edu.seckill.po.Perm;
import com.qfedu.edu.seckill.po.Role;
import com.qfedu.edu.seckill.po.User;
import com.qfedu.edu.seckill.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermMapper permMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String loginVoJson) throws UsernameNotFoundException {
        //上一次写的是什么代码?
        //首先通过用户名查询用户对象
        //通过用户id查询权限  通过用户id查询角色
        //最后封装成一个指定的对象 进行返回...
        //第一步：找到用户名
        LoginVo loginVo = JSON.parseObject(loginVoJson, LoginVo.class);
        String username = loginVo.getUsername();
        //第二步：通过用户名查询用户对象
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        User userSelect = userMapper.selectOne(userQueryWrapper);
        //判断是不是为空
        if(null==userSelect){
             throw new UsernameNotFoundException("用户名不对");
        }
        //执行到这里 说明用户名是对的...通过用户id查询权限 通过用户id查询角色
        List<Perm> perms=permMapper.selectPermsByUserId(userSelect.getUserId());
        //通过用户id查询角色
        List<Role> roles=roleMapper.selectRolesBVyUserId(userSelect.getUserId());
        //将权限信息和角色信息弄成 List<String>
        List<String> roleAndPermStringList=wrapRoleAndPerm(perms,roles);
        //将用户信息  权限信息 角色信息 封装成一个指定对象 进行返回...
        SecurityUser securityUser = new SecurityUser();
        securityUser.setUser(userSelect);
        securityUser.setType(loginVo.getType());
        securityUser.setRoleAndPermStringList(roleAndPermStringList);
        return securityUser;
    }

    /**
     * 将权限信息和角色信息弄成List<String>的样式
     * @param perms
     * @param roles
     * @return
     */
    private List<String> wrapRoleAndPerm(List<Perm> perms, List<Role> roles) {

        //申明容器
        List<String> lists=new ArrayList<>();
        for (int i = 0; i <perms.size() ; i++) {
            Perm perm = perms.get(i);
            lists.add(perm.getPermVal());
        }
        for (int i = 0; i <roles.size() ; i++) {
            Role role = roles.get(i);
            lists.add("ROLE_"+role.getRoleName());
        }
        return lists;
    }
}
